#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e5 + 10, INF = 0x3f3f3f3f;

ll n, ans;
ll w[N], cnt[N];
vector<ll> g[N], node;
map<ll, vector<ll>> hs;
struct DSU {
    std::vector<ll> f, sz;
    DSU() {}
    DSU(ll n) { init(n); }
    void init(ll n) {
        n++;
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        sz.assign(n, 1);
    }
    ll find(ll x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    ll size(ll x) {
        return sz[find(x)];
    }
    bool merge(ll x, ll y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        sz[x] += sz[y];
        f[y] = x;
        return true;
    }
    bool same(ll x, ll y) {
        return find(x) == find(y);
    }
};

void solve()
{
    cin >> n;
    for (ll i = 1;i <= n;i++) cin >> w[i];
    for (ll i = 1;i <= n;i++) hs[w[i]].push_back(i);
    for (ll i = 1;i < n;i++) {
        ll u, v;cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        if (w[u] == w[v]) ans += 2;
    }

    DSU dsu(n);
    for (auto it = hs.rbegin();it != hs.rend();it++) {
        node.clear();
        auto& p = it->second;
        for (ll x : p)
            for (ll y : g[x]) {
                if (w[y] <= w[x]) continue;
                ll fa = dsu.find(y);
                if (cnt[fa] == 0) node.push_back(fa);
                cnt[fa]++;
            }
        for (ll x : node) {
            ans += cnt[x] * (cnt[x] - 1);
            cnt[x] = 0;
        }
        for (ll x : p)
            for (ll y : g[x])
                if (w[y] >= w[x])
                    dsu.merge(y, x);
    }
    cout << ans << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}